package com.sdry.web.controller.hpf;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sdry.model.hpf.CacheArea;
import com.sdry.model.hpf.Page;
import com.sdry.service.hpf.CacheAreaService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * @ClassName: CacheAreaController 
 * @Description:缓存区Controller
 * @Author hpf
 * @Date 2018年9月19日 上午11:44:31 
 * @version 1.0
 */
@Controller
@RequestMapping(value="/cacheArea")
public class CacheAreaController {

	@Resource
	private CacheAreaService cacheAreaService;
	
	/**
	 * 分页查询缓存区
	 * @param page 分页条件
	 * @param CacheArea 查询条件
	 * @param response
	 */
	@RequestMapping(value="/listPageCacheArea")
	public void listPageCacheArea(Page page,CacheArea cacheArea,HttpServletResponse response){
		Map<String, Object> map = new HashMap<>();
		map.put("page", page.getPage());
		map.put("limit", page.getLimit());
		if (cacheArea.getPanum() != null) {
			map.put("panum", cacheArea.getPanum());
		} else {
			map.put("panum", "");
			cacheArea.setPanum("");
		}
		List<CacheArea> cacheAreaList = cacheAreaService.listPageCacheArea(map);
		int count = cacheAreaService.countCacheArea(cacheArea);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(cacheAreaList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 缓存区叫料
	 * @param paID 配件ID
	 * @param batch 批次号
	 * @param quantity 数量
	 * @param response
	 * @param aession
	 */
	@RequestMapping(value="/cacheAreaIn")
	public void cacheAreaIn(Long[] paID,String[] batch,String[] quantity,HttpServletResponse response,HttpSession session){
		int isSuccess = 0;
		isSuccess = cacheAreaService.insertCacheArea(paID, batch, quantity, session);
		try {
			ResponseUtil.write(response, isSuccess);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 缓存区盘点
	 * @param list
	 * @param response
	 */
	@RequestMapping(value="/saveCacheArea")
	public void saveCacheArea(String list,HttpServletResponse response){
		int isSuccess = 0;
		JSONArray ary = JSONArray.fromObject(list);
		if(ary != null && ary.size() > 0){
            List<CacheArea> cacheAreaList = (List<CacheArea>)JSONArray.toCollection(ary, 
                    CacheArea.class);
            isSuccess = cacheAreaService.cacheAreaCheck(cacheAreaList);
        }
		try {
			ResponseUtil.write(response, isSuccess);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 缓存区盘点清空
	 * @param list
	 * @param response
	 */
	@RequestMapping(value="/saveCacheArea2")
	public void saveCacheArea2(String list,HttpServletResponse response){
		int isSuccess = 0;
		JSONArray ary = JSONArray.fromObject(list);
		if(ary != null && ary.size() > 0){
			List<CacheArea> cacheAreaList = (List<CacheArea>)JSONArray.toCollection(ary, 
					CacheArea.class);
			isSuccess = cacheAreaService.cacheAreaEmpty(cacheAreaList);
		}
		try {
			ResponseUtil.write(response, isSuccess);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 设置预警值
	 * @param cacheArea 缓存区实体
	 * @return
	 */
	@RequestMapping(value="/setWarningValue")
	public void setWarningValue(CacheArea cacheArea,HttpServletResponse response){
		int isSuccess = cacheAreaService.setWarningValue(cacheArea);
		try {
			ResponseUtil.write(response, isSuccess);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
